* Kristoffer Balle Hvidberg
* Field of Study and Financial Problems: How Economics Reduces the Risk of Default

********************************************************************************

clear all
set more off

global data "K:\Workdata\704365\kbh\FSFP_Replication_Package\data"
global temp "K:\Workdata\704365\kbh\FSFP_Replication_Package\data\temp"
global raw  "K:\Workdata\704365\kbh\FSFP_Replication_Package\data\raw"

****************************************************************
* DATA WITH CLASSIFICATIONS OF CURRENT AND COMPLETED EDUCATION *
****************************************************************

	use "$raw\c_udd_udd_l1l1_t", clear
		merge 1:1 start using "$raw\c_udd_niveau_l1l2_t", nogen
		merge 1:1 start using "$raw\c_udd_field_l1l2_t", nogen
		merge 1:1 start using "$raw\c_udd_field_l1l3_t", nogen
		merge 1:1 start using "$raw\c_udd_field_l1l4_t", nogen
		rename start 				udd
		rename UDD_UDD_L1L1_T 		udd_navn
		rename UDD_NIVEAU_L1L2_T	udd_niveau
		rename UDD_FIELD_L1L2_T		udd_field2
		rename UDD_FIELD_L1L3_T		udd_field3
		rename UDD_FIELD_L1L4_T		udd_field4
	save "$temp\udd_niveau_field"
		
	
	use "$raw\c_audd_audd_l1l1_t", clear
		merge 1:1 start using "$raw\c_audd_niveau_l1l2_t", nogen
		merge 1:1 start using "$raw\c_audd_field_l1l2_t", nogen
		merge 1:1 start using "$raw\c_audd_field_l1l3_t", nogen
		merge 1:1 start using "$raw\c_audd_field_l1l4_t", nogen
		rename start 				audd
		rename AUDD_AUDD_L1L1_T 	audd_navn
		rename AUDD_NIVEAU_L1L2_T	audd_niveau
		rename AUDD_FIELD_L1L2_T	audd_field2
		rename AUDD_FIELD_L1L3_T	audd_field3
		rename AUDD_FIELD_L1L4_T	audd_field4
	save "$temp\audd_niveau_field"


**********************
* LINK KOTNR AND UDD *
**********************

	* Get current education, udd, for all individuals
		use "$temp\registerdata", clear
		keep pnr year udd udd_n
		keep if inrange(year,1993,2006)
		save "$temp\udd"
	
	* Load central admissions data
		use "$raw\kot2016.dta", clear

	* Keep relevant variables and observations
		keep pnr aar optagetjn kotnr
		drop if mi(pnr)
		drop if optagetjn==0 // Only use application when applicant was accepted

		bysort pnr aar: egen sum=sum(optagetjn) // Drop applicants accepted to several programs
		drop if sum>1 
		drop sum

	* Rename and sort before merge
		rename aar year
		sort kotnr year
		destring kotnr, replace

	* Name of programs from the central admission
		merge m:1 kotnr year using "$raw\kot_hovedtal"

	* Keep relevant variables and observations
		drop optagetjn UDD_GRP kot_institution adgangskvotient UDD_TYPE UVM_FAG _merge
		drop if year>2006
		drop if mi(pnr)
		duplicates report pnr year

	* Merge with current education
		merge 1:1 pnr year using "$temp\udd.dta"

	* Keep relevant variables and observations
		keep if !mi(kotnr)
		drop _merge
		
	* Current education code is stored in another variable in 2006
		tostring udd_n, gen(udd_c) format(%04.0f)
		replace udd_c="" if udd_c=="."
		replace udd = udd_c if mi(udd) & !mi(udd_c)
		drop udd_*

	* Names of current educations
		sort udd
		merge m:1 udd using "$temp\udd_niveau_field"
		sort year kotnr udd
		drop if _merge==2
		drop _merge
		drop if mi(udd)

	* For each KOT number in each year: Find the the current education that most of the accepted applicants have enrolled in. 
		bysort year kotnr: egen obs = count(pnr)
		sum obs if kotnr!=kotnr[_n-1] | year!=year[_n-1],d
		keep if obs>6
		bysort year kotnr: egen udd_mode		= mode(udd) 
		bysort year kotnr: egen udd_navn_mode 	= mode(udd_navn)
		bysort year kotnr: egen udd_niveau_mode	= mode(udd_niveau)
		bysort year kotnr: egen udd_field2_mode	= mode(udd_field2) 
		bysort year kotnr: egen udd_field3_mode	= mode(udd_field3) 
		bysort year kotnr: egen udd_field4_mode	= mode(udd_field4) 
		
	* Keep relevant variables and observations (unique match between udd and kotnr)
		drop pnr udd UDD_TX udd_navn udd_field2 udd_field3 udd_field4 udd_niveau obs

		duplicates drop
		duplicates report kotnr year
	
		label var year ""
		label var kotnr ""
		label var kot_navn ""
		
		rename udd_mode 		udd	
		rename udd_navn_mode	udd_navn
		rename udd_niveau_mode	udd_niveau
		rename udd_field2_mode 	udd_field2
		rename udd_field3_mode	udd_field3
		rename udd_field4_mode	udd_field4
		
	* Save
		save "$temp\link_kotnr_udd_niveau_field.dta", replace
		
		
***********************
* LINK KOTNR AND MATH *
***********************

	* Get completed education, udd, for all individuals
	use "$temp\registerdata", clear
		keep pnr year hfaudd
	save "$temp\hfaudd"

	* Load central admissions data
	use "$raw\kot2016.dta", clear

	* Keep relevant variables and observations 
		destring pnr, gen(pnr_num) force
		drop if mi(pnr_num) // "Fake" pnr
		drop pnr_num

		keep pnr aar optagetjn kotnr
		drop if mi(pnr)
		drop if optagetjn==0 // Only use application when applicant was accepted

		bysort pnr aar: egen sum=sum(optagetjn) // Drop applicants accepted to several programs
		drop if sum>1 
		drop sum

	* Rename, sort and merge
		rename aar year
		sort kotnr year
		destring kotnr, replace
		merge m:1 kotnr year using "$raw\kot_hovedtal"

	* Keep relevant variables and observations 
		drop optagetjn UDD_GRP adgangskvotient UDD_TYPE UVM_FAG _merge
		drop if year>2006
		drop if mi(pnr)
		duplicates report pnr year

	* Merge with completed education
		merge 1:1 pnr year using "$temp\hfaudd.dta"
		drop _merge
		
		tostring hfaudd, gen(audd) format(%04.0f)
		replace audd="" if audd=="."
		sort audd
		
		merge m:1 audd using "$temp\audd_niveau_field"
		drop if _merge==2
		drop _merge
		
	* Dummies for educational level
		tab audd_niveau, gen(niv)
		
	* Sort
		sort audd pnr year
	
	* Generate indicator for math track in upper secondary school
		gen mat1 = strpos(audd_navn, "matematisk") > 0
		gen mat2 = strpos(audd_navn, "Matematisk") > 0
		gen mat3 = strpos(audd_navn, "matematik") > 0
		gen mat4 = strpos(audd_navn, "Matematik") > 0
		gen mat5 = strpos(audd_navn, "htx") > 0
		gen mat6 = strpos(audd_navn, "Htx") > 0
		gen 	temp_mat = 1 if (mat1==1 | mat2==1 | mat3==1 | mat4==1 | mat5==1 | mat6==1) & niv3==1  & !mi(audd)
		replace temp_mat = 0 if mi(temp_mat) & !mi(audd)
		
	* Keep relevant variables and observations 
		drop mat* niv* UDD_TX
		
	* Indicator for all observations for each applicants
		sort pnr year
		by pnr: egen mat=max(temp_mat)
		drop temp_mat audd_navn audd_niveau audd_field*
	
	* Keep observations with non-missing kotnr
		drop if mi(kotnr)

	* Count number of accepted applicants by program/year
		sort kotnr year
		by kotnr year: egen antal_opt = count(kotnr)
		sum antal_opt, d
		
	* Compute share from math track if at least 10 applicants were accepted
		by kotnr year: egen mat_frac_temp = mean(mat)           if antal_opt>=10
		by kotnr year: egen mat_frac      = mean(mat_frac_temp) 

	* Keep one program/year observation
		drop pnr hfaudd audd mat mat_frac_temp
		duplicates drop
		duplicates report kotnr year
	
	* Calculate average share of applicants from math track for each program across years
		sort kotnr year
		count if kotnr==kotnr[_n-1] & kot_navn!=kot_navn[_n-1]
		bysort kotnr kot_navn kot_institution: egen mat_mean=mean(mat_frac)
	
	* Keep relevant variables
		keep year kotnr mat*

	* Check for duplicates
		duplicates report kotnr year
		
	* Labels
		label var mat_frac "Fraction of math applicants"
		label var mat_mean "Average of fraction of math applicants across years"
	
	* Save
		save "$temp\link_kotnr_mat.dta"

*******************
* AUDD AND FIELDS *
******************'

	* Define fields of study used in paper for completed educations
		use "$temp\audd_niveau_field", clear
		
		gen stem = 0
		gen oeko = 0
		gen jura = 0
		gen huaa = 0
		gen educ = 0
		gen welf = 0
		gen medi = 0
		gen sosc = 0
		
		replace stem = 1 if audd_field4=="Engineering, manufacturing and construction"
		replace stem = 1 if audd_field4=="Natural sciences, mathematics and statistics"
		replace stem = 1 if audd_field4=="Information and communication technologies (ICTs)"
		replace stem = 0 if audd_field2=="Architecture and town planning"
		replace stem = 0 if audd_field2=="Inter-disciplinary programmes and qualifications involving mathematics and statistics" // Mat-øk
		
		replace oeko = 1 if audd_field2=="Inter-disciplinary programmes and qualifications involving mathematics and statistics" // Mat-øk
		replace oeko = 1 if audd_field2=="Economics"
		replace oeko = 1 if audd_field3=="Business and administration"
		replace oeko = 1 if audd_field2=="Inter-disciplinary programmes and qualifications involving agriculture"
		
		replace jura = 1 if audd_field3=="Law"
		
		replace huaa = 1 if audd_field4=="Arts and humanities"
		replace huaa = 1 if audd_field2=="Architecture and town planning"
		
		replace educ = 1 if audd_field4=="Education"
		
		replace welf = 1 if audd_field4=="Health and welfare"
		replace welf = 0 if audd_field2=="Medicine"
		
		replace medi = 1 if audd_field2=="Medicine"
		replace medi = 1 if audd_field3=="Veterinary " 
		
		replace sosc = 1 if audd_field4=="Social sciences, journalism and information"
		replace sosc = 0 if audd_field2=="Economics"
		
		keep audd stem oeko jura huaa educ welf medi sosc
		
		save "$temp\link_audd_stem"
		
*****************
* UDD OG FIELDS *
***************	*

	* Define fields of study used in paper for current educations
		use "$temp\udd_niveau_field", clear
		
		gen stem = 0
		gen oeko = 0
		gen jura = 0
		gen huaa = 0
		gen educ = 0
		gen welf = 0
		gen medi = 0
		gen sosc = 0
		
		replace stem = 1 if udd_field4=="Engineering, manufacturing and construction"
		replace stem = 1 if udd_field4=="Natural sciences, mathematics and statistics"
		replace stem = 1 if udd_field4=="Information and communication technologies (ICTs)"
		replace stem = 0 if udd_field2=="Architecture and town planning"
		replace stem = 0 if udd_field2=="Inter-disciplinary programmes and qualifications involving mathematics and statistics" // Mat-øk
		
		replace oeko = 1 if udd_field2=="Inter-disciplinary programmes and qualifications involving mathematics and statistics" // Mat-øk
		replace oeko = 1 if udd_field2=="Economics"
		replace oeko = 1 if udd_field3=="Business and administration"
		replace oeko = 1 if udd_field2=="Inter-disciplinary programmes and qualifications involving agriculture"
		
		replace jura = 1 if udd_field3=="Law"
		
		replace huaa = 1 if udd_field4=="Arts and humanities"
		replace huaa = 1 if udd_field2=="Architecture and town planning"
		
		replace educ = 1 if udd_field4=="Education"
		
		replace welf = 1 if udd_field4=="Health and welfare"
		replace welf = 0 if udd_field2=="Medicine"
		
		replace medi = 1 if udd_field2=="Medicine"
		replace medi = 1 if udd_field3=="Veterinary " 
		
		replace sosc = 1 if udd_field4=="Social sciences, journalism and information"
		replace sosc = 0 if udd_field2=="Economics"
		
		keep udd stem oeko jura huaa educ welf medi sosc
		
		save "$temp\link_udd_stem"
		
		
*****************
* AUDD AND ECON *
*****************
	
	* Completed econ educations as in Chetty et al (2014)
		use "$temp\audd_niveau_field", clear		
		
		destring audd, replace

		gen			econ=0

		replace econ=1 if audd==2490					/* Agrarøkonom */
		replace econ=1 if inrange(audd, 3809,3834) 		/* Merkonom */
		replace econ=1 if inrange(audd, 3837,3839) 		/* Akademimerk */
		replace econ=1 if audd==3843 					/* Akademiudd, finansiel råd. */ 
		replace econ=1 if audd==3845					/* Revisor */
		replace econ=1 if audd==3875					/* Akademiøkonom */
		replace econ=1 if audd==3910					/* Merkatil */
		replace econ=1 if audd==3915					/* Merkatil */
		replace econ=1 if inrange(audd,3970,4008)		/* Diverse korte handelsudd. */
		replace econ=1 if audd==4011					/* Akm.økon.,intern. handel */
		replace econ=1 if audd==4012					/* Akm.økon.,intern. marked */
		replace econ=1 if inrange(audd,4020,4028)		/* Diverse Akm.økon. */
		replace econ=1 if audd==4030					/* Akm.økon. una */
		replace econ=1 if audd==4193					/* Finansuddannelse */
		replace econ=1 if audd==4198					/* Hg. det merkantile område */
		replace econ=1 if audd==4200					/* Hg. una */
		replace econ=1 if audd==4203					/* Finansuddannelse una */
		replace econ=1 if audd==4758					/* Merkantil, individuel */
		replace econ=1 if inrange(audd,4964,4967)		/* Handelsassistent */
		replace econ=1 if audd==4977					/* Kontor, økonomi */
		replace econ=1 if audd==4983					/* Kontor, regnskab/revision */
		replace econ=1 if audd==4986					/* Pengeinstitut */
		replace econ=1 if audd==4987					/* Finansassistent */
		replace econ=1 if audd==4988					/* Forsikring */
		replace econ=1 if audd==4998					/* Realkredit */
		replace econ=1 if inrange(audd,5003,5009)		/* Akm.økon., diverse */
		replace econ=1 if audd==5020					/* Akm.økon. bilbranchen */
		replace econ=1 if audd==5028					/* Hhx, 1. år */
		replace econ=1 if audd==5029					/* Hhx, 2. år */
		replace econ=1 if inrange(audd,5076,5079)		/* Akm.økon., diverse */
		replace econ=1 if audd==5085					/* Akm.økon., kommunikation/formidling */
		replace econ=1 if inrange(audd,5093,5096)		/* Akm.økon., diverse */
		replace econ=1 if audd==5121					/* Finansøkonom */
		replace econ=1 if audd==5122					/* Handelsøkonom */
		replace econ=1 if audd==5123					/* Markedsføringsøkonom */
		replace econ=1 if audd==5124					/* Serviceøkonom */
		replace econ=1 if audd==5125					/* Administrationsøkonom */
		replace econ=1 if audd==5143					/* Markedsføringsøkonom */
		replace econ=1 if audd==5328					/* Teknologi og økonomi, diploming. prof.bach. */
		replace econ=1 if audd==5401					/* Jordbrugsøkonomi */
		replace econ=1 if audd==5490					/* Finans, prof.bach. */
		replace econ=1 if audd==5494					/* Design-business, prof.bach. */
		replace econ=1 if audd==5651					/* HA erhvervsret, bach. */
		replace econ=1 if audd==5652					/* Erhvervsret, c.merc. */
		replace econ=1 if audd==5653					/* IBP, c.merc. */
		replace econ=1 if audd==5656					/* HA datalogi, bach. */
		replace econ=1 if audd==5657					/* Datalogi, c.merc. */
		replace econ=1 if audd==5661					/* HA sprog, bach */
		replace econ=1 if audd==5662					/* Interpret, c.merc. */
		replace econ=1 if audd==5663					/* HA tysk, bach. */
		replace econ=1 if audd==5666					/* HA matematik, bach. */
		replace econ=1 if audd==5667					/* Matematik, c.merc. */
		replace econ=1 if audd==5685					/* Økonomi-IT, prof.bach. */
		replace econ=1 if audd==5687					/* Pro.Øk.-værdi, prof.bach */
		replace econ=1 if audd==5700					/* Erhvervsøkonomi HA, bach. */
		replace econ=1 if audd==5701					/* HA informatik, bach */
		replace econ=1 if audd==5702					/* HA kommunikation, bach */
		replace econ=1 if audd==5703					/* Erhvervsøkonomi, c.merc. */
		replace econ=1 if audd==5704					/* HA sprog, bach */
		replace econ=1 if audd==5705					/* Erhvervsøkonomi, phd */
		replace econ=1 if audd==5707					/* Registreret revisor */
		replace econ=1 if audd==5708					/* Revisorkand, c.merc.aud */
		replace econ=1 if audd==5709					/* HA erhvervsanalyse, bach */
		replace econ=1 if inrange(audd,5710,5734)		/* Diverse HA og HD */
		replace econ=1 if audd==5751					/* Business adm. (MBA), mast. */
		replace econ=1 if audd==5783					/* HA intern.erhv.øko/pol */
		replace econ=1 if inrange(audd,5785,5789)		/* Diverse cand.merc. */
		replace econ=1 if audd==7030					/* Forsikringsvidenskab, phd */
		replace econ=1 if audd==7045					/* Statsvidenskab, c.polit. */
		replace econ=1 if audd==7050					/* Statsvidenskab, phd */
		replace econ=1 if audd==7062					/* Økonomi, bach. */
		replace econ=1 if audd==7065					/* Økonomi, c.oecon */
		replace econ=1 if audd==7070					/* Økonomi, phd */
		replace econ=1 if audd==7079					/* Int. ret-øko-led, c.soc */
		replace econ=1 if audd==8059					/* Forsikringsvidenskab, bach. */
		replace econ=1 if audd==8133					/* Matematik-økonomi, bach. */
		replace econ=1 if audd==8301					/* Matematik-økonomi, phd */
		replace econ=1 if audd==8404					/* Jordbrugsøkonomi, bach. */
		replace econ=1 if audd==8601					/* Skattefaglig diplom */
		replace econ=1 if audd==8844					/* Skat, master */
		replace econ=1 if audd==8848					/* Business adm. (MBA), master */
		replace econ=1 if audd==8928					/* Business adm,exec., master */
		replace econ=1 if audd==9779					/* Fag. økonomi/regnskab */
		replace econ=1 if audd==9785					/* Fag. finanssektoren */

		drop audd_*
	
		tostring audd, replace format(%04.0f)
	
		save "$temp\link_audd_econ"
		
************
* UDD ECON *
************
	
	* Current econ educations as in Chetty et al (2014)
		use "$temp\udd_niveau_field", clear		
		
		destring udd, replace

		gen			econ=0

		replace econ=1 if udd==2490						/* Agrarøkonom */
		replace econ=1 if udd==3810						/* Merkonom */
		replace econ=1 if inrange(udd, 3838,3839) 		/* Akademimerk */
		replace econ=1 if udd==3843 					/* Akademiudd, finansiel råd. */ 
		replace econ=1 if udd==3845						/* Revisor */
		replace econ=1 if udd==3875						/* Akademiøkonom */
		replace econ=1 if udd==3910						/* Merkatil */
		replace econ=1 if udd==3915						/* Merkatil */
		replace econ=1 if inrange(udd,3970,4006)		/* Diverse korte handelsudd. */
		replace econ=1 if udd==4011						/* Akm.økon.,intern. handel */
		replace econ=1 if udd==4012						/* Akm.økon.,intern. marked */
		replace econ=1 if inrange(udd,4020,4028)		/* Diverse Akm.økon. */
		replace econ=1 if udd==4193						/* Finansuddannelse */
		replace econ=1 if udd==4198						/* Hg. det merkantile område */
		replace econ=1 if udd==4758						/* Merkantil, individuel */
		replace econ=1 if inrange(udd,5003,5009)		/* Akm.økon., diverse */
		replace econ=1 if udd==5020						/* Akm.økon. bilbranchen */
		replace econ=1 if inrange(udd,5076,5079)		/* Akm.økon., diverse */
		replace econ=1 if udd==5085						/* Akm.økon., kommunikation/formidling */
		replace econ=1 if inrange(udd,5093,5096)		/* Akm.økon., diverse */
		replace econ=1 if udd==5121						/* Finansøkonom */
		replace econ=1 if udd==5122						/* Handelsøkonom */
		replace econ=1 if udd==5123						/* Markedsføringsøkonom */
		replace econ=1 if udd==5124						/* Serviceøkonom */
		replace econ=1 if udd==5125						/* Administrationsøkonom */
		replace econ=1 if udd==5143						/* Markedsføringsøkonom */
		replace econ=1 if udd==5328						/* Teknologi og økonomi, diploming. prof.bach. */
		replace econ=1 if udd==5401						/* Jordbrugsøkonomi */
		replace econ=1 if udd==5490						/* Finans, prof.bach. */
		replace econ=1 if udd==5494						/* Design-business, prof.bach. */
		replace econ=1 if udd==5651						/* HA erhvervsret, bach. */
		replace econ=1 if udd==5652						/* Erhvervsret, c.merc. */
		replace econ=1 if udd==5653						/* IBP, c.merc. */
		replace econ=1 if udd==5654						/* TILFØJET AF K: HA jur.  */
		replace econ=1 if udd==5656						/* HA datalogi, bach. */
		replace econ=1 if udd==5657						/* Datalogi, c.merc. */
		replace econ=1 if udd==5658						/* TILFØJET AF K: HA jur.  */
		replace econ=1 if udd==5661						/* HA sprog, bach */
		replace econ=1 if udd==5662						/* Interpret, c.merc. */
		replace econ=1 if udd==5663						/* HA tysk, bach. */
		replace econ=1 if udd==5664						/* TILFØJET AF K: Økonomi og info.tek.*/
		replace econ=1 if udd==5665						/* TILFØJET AF K: Bus.econ. and Info. Tech. */
		replace econ=1 if udd==5666						/* HA matematik, bach. */
		replace econ=1 if udd==5667						/* Matematik, c.merc. */
		replace econ=1 if udd==5685						/* Økonomi-IT, prof.bach. */
		replace econ=1 if udd==5687						/* Pro.Øk.-værdi, prof.bach */
		replace econ=1 if udd==5700						/* Erhvervsøkonomi HA, bach. */
		replace econ=1 if udd==5701						/* HA informatik, bach */
		replace econ=1 if udd==5702						/* HA kommunikation, bach */
		replace econ=1 if udd==5703						/* Erhvervsøkonomi, c.merc. */
		replace econ=1 if udd==5704						/* HA sprog, bach */
		replace econ=1 if udd==5705						/* Erhvervsøkonomi, phd */
		replace econ=1 if udd==5707						/* Registreret revisor */
		replace econ=1 if udd==5708						/* Revisorkand, c.merc.aud */
		replace econ=1 if udd==5709						/* HA erhvervsanalyse, bach */
		replace econ=1 if inrange(udd,5710,5734)		/* Diverse HA og HD */
		replace econ=1 if udd==5751						/* Business adm. (MBA), mast. */
		replace econ=1 if inrange(udd,5780,5782)		/* TILFØJET AF K */
		replace econ=1 if udd==5783						/* HA intern.erhv.øko/pol */
		replace econ=1 if inrange(udd,5785,5789)		/* Diverse cand.merc. */
		replace econ=1 if udd==7020						/* TILFØJET AF K: Forsikringsvidenskab */
		replace econ=1 if udd==7030						/* Forsikringsvidenskab, phd */
		replace econ=1 if udd==7045						/* Statsvidenskab, c.polit. */
		replace econ=1 if udd==7050						/* Statsvidenskab, phd */
		replace econ=1 if udd==7060						/* Økonomi, kand. */
		replace econ=1 if udd==7062						/* Økonomi, bach. */
		replace econ=1 if udd==7065						/* Økonomi, c.oecon */
		replace econ=1 if udd==7070						/* Økonomi, phd */
		replace econ=1 if udd==7079						/* Int. ret-øko-led, c.soc */
		replace econ=1 if udd==7100						/* TILFØJET AF K: Politik og økonomi */
		replace econ=1 if udd==8059						/* Forsikringsvidenskab, bach. */
		replace econ=1 if udd==8133						/* Matematik-økonomi, bach. */
		replace econ=1 if udd==8301						/* Matematik-økonomi, phd */
		replace econ=1 if udd==8404						/* Jordbrugsøkonomi, bach. */
		replace econ=1 if udd==8601						/* Skattefaglig diplom */
		replace econ=1 if udd==8844						/* Skat, master */
		replace econ=1 if udd==8845						/* TILFØJET AF K: Skat, master */
		replace econ=1 if udd==8848						/* Business adm. (MBA), master */
		replace econ=1 if udd==8928						/* Business adm,exec., master */
		replace econ=1 if udd==9779						/* Fag. økonomi/regnskab */
		replace econ=1 if udd==9785						/* Fag. finanssektoren */

		drop udd_*
	
		tostring udd, replace format(%04.0f)
	
		save "$temp\link_udd_econ"
		
		
*********
* MERGE *
*********

		use "$temp\link_kotnr_udd_niveau_field", clear
		merge 1:1 kotnr year using "$temp\link_kotnr_mat"
		drop _merge
		sort udd
		merge m:1 udd using "$temp\link_udd_stem"
		drop if _merge==2
		drop _merge
		merge m:1 udd using "$temp\link_udd_econ"
		drop if _merge==2
		drop _merge
		
		rename udd_field2 	kot_field_l2
		rename udd_field3 	kot_field_l3
		rename udd_field4 	kot_field_l4
		rename udd_niveau 	kot_niveau
		rename mat_frac 	kot_mat1
		rename mat_mean 	kot_mat2
		rename stem			kot_stem
		rename oeko			kot_oeko
		rename jura			kot_jura
		rename educ			kot_educ
		rename welf			kot_welf
		rename medi			kot_medi
		rename sosc			kot_sosc
		rename huaa  		kot_huaa  
		rename econ			kot_econ
		
		drop udd*
		drop kot_navn
		
		sort kotnr year
		
		save "$temp\kotnr_niveau_field"
		
		rename kotnr 			kotnr1
		rename kot_field_l2		kot1_field_l2		
		rename kot_field_l3     kot1_field_l3
		rename kot_field_l4     kot1_field_l4
		rename kot_niveau       kot1_niveau
		rename kot_mat1         kot1_mat1
		rename kot_mat2         kot1_mat2
		rename kot_stem      	kot1_stem
		rename kot_oeko         kot1_oeko
		rename kot_jura			kot1_jura
		rename kot_educ         kot1_educ
		rename kot_welf     	kot1_welf
		rename kot_medi     	kot1_medi
		rename kot_sosc    		kot1_sosc
		rename kot_huaa         kot1_huaa   
		rename kot_econ			kot1_econ
		
		save "$temp\kotnr1", replace
		
		use "$temp\kotnr_niveau_field", clear
		
		rename kotnr 			kotnr2
		rename kot_field_l2		kot2_field_l2		
		rename kot_field_l3     kot2_field_l3
		rename kot_field_l4     kot2_field_l4
		rename kot_niveau       kot2_niveau
		rename kot_mat1         kot2_mat1
		rename kot_mat2         kot2_mat2
		rename kot_stem      	kot2_stem
		rename kot_oeko         kot2_oeko
		rename kot_jura			kot2_jura
		rename kot_educ         kot2_educ
		rename kot_welf     	kot2_welf
		rename kot_medi     	kot2_medi
		rename kot_sosc    		kot2_sosc
		rename kot_huaa         kot2_huaa   
		rename kot_econ			kot2_econ
		
		save "$temp\kotnr2", replace

